package com.atguigu.flink04;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2024/2/22
 * 连接两条流，输出能根据id匹配上的数据（类似inner join效果）
 */
public class Flink07_Connect_inner {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<Tuple2<Integer, String>> ds1 = env.fromElements(
                Tuple2.of(1, "a1"),
                Tuple2.of(1, "a2"),
                Tuple2.of(2, "b"),
                Tuple2.of(3, "c")
        );
        DataStreamSource<Tuple3<Integer, String, Integer>> ds2 = env.fromElements(
                Tuple3.of(1, "aa1", 1),
                Tuple3.of(1, "aa2", 2),
                Tuple3.of(2, "bb", 1),
                Tuple3.of(3, "cc", 1)
        );


        //关联两条流
        ConnectedStreams<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>> connectDS = ds1.connect(ds2);

        //对两条流进行分组   将相同key的数据放到一组进行处理
        ConnectedStreams<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>> cDS = connectDS.keyBy(
                t2 -> t2.f0,
                t3 -> t3.f0
        );

        //对流中数据进行处理
        SingleOutputStreamOperator<String> processDS = cDS.process(
                new CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>() {
                    Map<Integer, List<Tuple2<Integer, String>>> ds1Cache = new HashMap<>();
                    Map<Integer, List<Tuple3<Integer, String, Integer>>> ds2Cache = new HashMap<>();

                    //处理第一条流数据
                    @Override
                    public void processElement1(Tuple2<Integer, String> value, CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                        Integer id = value.f0;
                        if (ds1Cache.containsKey(id)) {
                            //直接将流中的这条数据 放到集合中缓存起来
                            ds1Cache.get(id).add(value);
                        } else {
                            List<Tuple2<Integer, String>> ds1List = new ArrayList<>();
                            ds1List.add(value);
                            ds1Cache.put(id, ds1List);
                        }

                        //到另一条中缓存的数据进行遍历，看看是否有能关联上的数据
                        if (ds2Cache.containsKey(id)) {
                            for (Tuple3<Integer, String, Integer> t3 : ds2Cache.get(id)) {
                                out.collect(value + "-----" + t3);
                            }
                        }
                    }

                    //处理第二条流数据
                    @Override
                    public void processElement2(Tuple3<Integer, String, Integer> value, CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                        Integer id = value.f0;
                        if (ds2Cache.containsKey(id)) {
                            ds2Cache.get(id).add(value);
                        } else {
                            List<Tuple3<Integer, String, Integer>> ds2List = new ArrayList<>();
                            ds2List.add(value);
                            ds2Cache.put(id, ds2List);
                        }

                        //用当前数据和另一条流中缓存的数据进行关联
                        if (ds1Cache.containsKey(id)) {
                            for (Tuple2<Integer, String> tuple2 : ds1Cache.get(id)) {
                                out.collect(tuple2 + "-----" + value);
                            }
                        }
                    }
                }
        );

        processDS.print();


        env.execute();
    }
}

